package com.huawei.fakedouyin.base

import android.app.Activity
import android.app.Application
import android.os.Build
import android.os.Bundle
import android.os.StrictMode
import android.os.StrictMode.VmPolicy
import android.util.Log
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import com.huawei.fakedouyin.manager.PlayManager
import com.huawei.fakedouyin.util.LiveDataSet
import com.huawei.fakedouyin.util.LogUtil
import com.huawei.fakedouyin.util.NetStateChangeObserver
import com.huawei.fakedouyin.util.NetworkChangeReceiver
import kotlin.properties.Delegates


/**
 * 作者: 王业通（wytaper495@qq.com）
 * 日期: {2023/6/15 9:57}
 * 备注:
 */
class BaseApplication : Application(), ViewModelStoreOwner {
    companion object {
        var Instance by Delegates.notNull<Application>()
        public var mActivityCount = 0
    }

    private val mAppViewModelStore by lazy { ViewModelStore() }

    override fun onCreate() {
        super.onCreate()
        Instance = this
        initFileProvider()
        //初始化播放器
        PlayManager.getInstance().init()
        //注册应用所有activity生命周期监听 达到监听应用是否在后台的效果
        registerActivityLifecycleCallbacks(AppLifecycleCallback())
        //网络断网联网监听
        NetworkChangeReceiver.registerReceiver(this)
        //全局监听网络状态变化
        NetworkChangeReceiver.registerObserver(object : NetStateChangeObserver {
            override fun onDisconnect() {
                LiveDataSet.netMessage.postValue(0)
            }

            override fun onMobileConnect() {
                if (LiveDataSet.netMessage.get() != null && LiveDataSet.netMessage.get() === 0) {
                    LiveDataSet.netMessage.postValue(1)
                }
            }

            override fun onWifiConnect() {
                if (LiveDataSet.netMessage.get() != null && LiveDataSet.netMessage.get() === 0) {
                    LiveDataSet.netMessage.postValue(2)
                }
            }
        })
    }

    private fun initFileProvider() {
    // 解决7.0以上版本的FileProvider问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val builder = VmPolicy.Builder()
            StrictMode.setVmPolicy(builder.build())
        }
    }

    @Override
    override fun getViewModelStore() = mAppViewModelStore


    public fun isBackground(): Boolean {
        return mActivityCount == 0
    }


    class AppLifecycleCallback : Application.ActivityLifecycleCallbacks {
        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            Log.d("LUO", "AppLifecycleCallback===onActivityCreated==${activity}")
        }

        override fun onActivityStarted(activity: Activity) {
            Log.d("LUO", "AppLifecycleCallback===onActivityStarted===${activity}")
            if (mActivityCount == 0) {
                LiveDataSet.isBackGround.postValue(false)

            }
            mActivityCount++
        }

        override fun onActivityResumed(activity: Activity) {
            Log.d("LUO", "AppLifecycleCallback===onActivityResumed===${activity}")
        }

        override fun onActivityPaused(activity: Activity) {
            Log.d("LUO", "AppLifecycleCallback===onActivityPaused===${activity}")
        }

        override fun onActivityStopped(activity: Activity) {
            Log.d("LUO", "AppLifecycleCallback===onActivityStopped===${activity}")
            mActivityCount--
            if (mActivityCount == 0) {
                LiveDataSet.isBackGround.postValue(true)
                LogUtil.Instance.logDebug("应用退出到后台")
            }

        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
            Log.d("LUO", "AppLifecycleCallback===onActivitySaveInstanceState===${activity}")
        }

        override fun onActivityDestroyed(activity: Activity) {
            Log.d("LUO", "AppLifecycleCallback===onActivityDestroyed===${activity}")
        }

    }

}